package com.jt9003.service.impl;

import com.jt9003.dao.Therapy;
import com.jt9003.pojo.*;
import com.jt9003.service.TherapyService;
import com.jt9003.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TherapyServiceimpl implements TherapyService {
    @Resource
    Therapy therapy;
    @Resource
    RedisUtil redisUtil;

    //查询当天已诊治挂号单信息
    @Override
    public List<Register> selectRegister() {
        return therapy.selectRegister();
    }

    //查询当天未诊治的挂号单信息
    @Override
    public List<Register> selectRegister2() {
        return therapy.selectRegister2();
    }

    //根据挂号单号查询挂号单信息
    @Override
    public Register selectRegister3(String registrationNumber) {
        return therapy.selectRegister3(registrationNumber);
    }

    //添加患者病例
    @Override
    public int addPatient_case_history(Patient_case_history pch) {
        //生成患者对应的病历号  根据时间和当前患者的id设置
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
        int pid = pch.getpId();
        String pCaseNumber=sdf.format(new Date())+pid;
        pch.setpCaseNumber(pCaseNumber);

        //设置当前时间
        pch.setpDiagnosisDate(new Date());

        return  therapy.addPatient_case_history(pch);
    }

    //查询病症
    @Override
    public List<Disease_tcm> selectDTinfo() {
        return therapy.selectDTinfo();
    }

    //添加新病症
    @Override
    public int addDisease(String diseaseName) {
        return therapy.addDisease(diseaseName);
    }

    //查询药品名和id，每天和每次使用次数
    @Override
    public List<Drugs> selectDrugsName() {
        return therapy.selectDrgusName();
    }

    //查询所有中成药
    @Override
    public List<Drugs> selectCTM() {
        return therapy.selectCTM();
    }

    //查询所有西药
    @Override
    public List<Drugs> selectWm() {
        return therapy.selectWm();
    }


    //查询所有体检项目
    @Override
    public List<Physical_examination> selectPE() {
        return therapy.selectPE();
    }

    //查询其他费用项目
    @Override
    public List<Other_price> selectOP() {
        return therapy.selectOP();
    }

    //根据id查询模板数据
    @Override
    public Template getTemplateByid(Integer id) {
        return therapy.getTemplatebyId(id);
    }

    //根据id获取院内协定方和药品的相关数据
    @Override
    public Template getAgreementPartyTemplateById(Integer id) {
        return therapy.getAgreementPartyTemplateById(id);
    }

    //根据id获取调理方数据
    @Override
    public Template getOrganize(Integer id) {
        return therapy.getOrganize(id);
    }

    //根据id查询患者病例
    @Override
    public List<Patient_case_history> selectOldCase(Integer id) {
        return therapy.selectOldCase(id);
    }

    //根据多个药品id查询药品总价
    @Override
    public Double selectTotalPrice(List<Integer> list) {
        return therapy.selectTotalPrice(list);
    }

    //根据多个id查询体检项目的总价
    @Override
    public Double selectPEprice(List<Integer> list) {
        return therapy.selectPEprice(list);
    }

    //处方表插入数据
    @Override
    public int addPrescription(Prescription prescription,String doctorName) {
        //获取当前登录的医生的信息
        int count = 0;
        if (doctorName!=null){
            prescription.setuDoctorName(doctorName);
            count = therapy.addPrescription(prescription);
        }
        return count;
    }

    //处方与药品关系表插入数据
    @Override
    public int addpreDRelationship(List<POTCM> list) {
        //给POTCM里 preConsumptionReal2 preConsumptionReal3 drType参数,因为sql
        for (POTCM potcm:list){
            //如果该对象没有该属性的数据则获取药品表里的数据
            if (potcm.getDr_consumption_day()==0&&potcm.getDr_consumption_times()==0){
                //获取药品的id,根据id查询药品数据放入对象
                int id= potcm.getDrId();
                Drugs drugs = therapy.selectDrugsInfo(id);
                potcm.setDr_consumption_day(Integer.parseInt(drugs.getDrConsumptionDay()));
                potcm.setDr_consumption_times(Integer.parseInt(drugs.getDrConsumptionTimes()));
            }
            if (potcm.getDrType()==0){
                int id= potcm.getDrId();
                Drugs drugs = therapy.selectDrugsInfo(id);
                potcm.setDrType(drugs.getDrType());
            }
        }
        return  therapy.addpreDRelationship(list);
    }

    //查询其他收费项目的价格
    @Override
    public Double selectOPprice(List<Integer> list) {
        return therapy.selectOPprice(list);
    }

    //修改挂号单例的患者诊疗状态
    @Override
    public int updataP_diagnosis(int rId) {
        return therapy.updataP_diagnosis(rId);
    }


}
